package io;

import org.junit.Test;

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

/**
 * @Author: zhenzhong.wang
 * @Time: 2018/4/26 11:38
 */
public class FileUtils {


  /**
   * 列出某个路径下所有的文件列表
   */
  @Test
  public void listAllFiles() {
    String path = "/Users/zhenzhong/nginx";
    File rootFile = new File(path);
    reviewFile(rootFile, 0);
  }

  public void reviewFile(File file, int level) {
    if (!file.isDirectory()) {
      System.out.println(getWhiteSpace(level) + file.getName());
      // 清除所有tab
//    clearTabInFile(file);

      // 替换四个空格为两个空格
//      transferToTwoIndent(file);

    } else {
      System.out.println(getWhiteSpace(level) + file.getName() + ":");

      if (file.listFiles() == null) {
        return;
      }

      for (File f : file.listFiles()) {
        reviewFile(f, level + 1);
      }
    }
  }

  public String getWhiteSpace(int level) {
    int length = level << 1;
    String space = "";
    if (length > 0) {
      for (int i = 0; i < length; i++) {
        space += "-";
      }
    }
    return space;
  }

  /**
   * 清除文件里的所有tab，改用四个空格代替
   *
   * @param file
   */
  public void clearTabInFile(File file) {
    try {
      BufferedReader br = new BufferedReader(new FileReader(file));
      StringBuffer data = new StringBuffer();
      String re;
      int sum = 0;
      while ((re = br.readLine()) != null) {
        if (re.contains("\t")) {
          sum++;
          re = re.replaceAll("\t", "  ");
        }
        data.append(re).append("\n");
      }
      br.close();
      if (sum > 0) {
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        bw.write(data.toString());
        bw.close();
      }

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  /**
   * 将文件的缩进改为四个空格
   *
   * @param file
   */
  public void transferToTwoIndent(File file) {
    try {
      BufferedReader br = new BufferedReader(new FileReader(file));
      StringBuffer data = new StringBuffer();
      String re;
      int sum = 0;
      while ((re = br.readLine()) != null) {
        if (re.contains("  ")) {
          sum++;
          re = re.replaceAll("  ", "  ");
        }
        data.append(re).append("\n");
      }
      br.close();
      if (sum > 0) {
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        bw.write(data.toString());
        bw.close();
      }

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }


  /**
   * 使用NIO方式列出所有文件
   *
   * @throws IOException
   */
  @Test
  public void listAllFilesByNIO() throws IOException {
    String path = "G:\\WEB-INF";
    Path pa = Paths.get(path);
    Files.walkFileTree(pa, new SimpleFileVisitor<Path>() {

      @Override
      public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        System.out.println(file.getFileName());
        return FileVisitResult.CONTINUE;
      }

      @Override
      public FileVisitResult postVisitDirectory(Path file, IOException exc) {
        System.out.println(file.getFileName());
        return FileVisitResult.CONTINUE;
      }
    });
  }
}
